import datetime
import imaplib, email
from email.header import decode_header


class EmailHelper:
    def make_seen(self, username, password):
        mail = imaplib.IMAP4_SSL('imap.{}'.format(username.split("@")[1]))
        mail.login(username, password)

        args = ("name", "XXXX", "contact", "XXXX@163.com", "version", "1.0.0", "vendor", "myclient")
        typ, dat = mail._simple_command('ID', '("' + '" "'.join(args) + '")')
        # print(mail.)
        mail.select('inbox')
        result, data = mail.uid('search', None, "UNSEEN")  # (ALL/UNSEEN)
        i = len(data[0].split())  # emails count
        for x in range(i):
            latest_email_uid = data[0].split()[x]
            mail.uid('fetch', latest_email_uid, '(RFC822)')

    def read_email(self, username, password):
        mail = imaplib.IMAP4_SSL('imap.{}'.format(username.split("@")[1]))
        mail.login(username, password)
        imaplib.Commands["ID"] = ("AUTH",)
        args = ("name", username, "contact", username, "version", "1.0.0", "vendor", "myclient")
        mail._simple_command("ID", str(args).replace(",", "").replace("\'", "\""))
        mail.select('inbox')

        result, data = mail.uid('search', None, "ALL")

        mail_ids = data[0]
        id_list = mail_ids.split()
        latest_email_id = id_list[-1]  #

        # 获取邮件数据
        result, email_data = mail.uid('fetch', latest_email_id, '(BODY.PEEK[])')

        raw_email = email_data[0][1]
        # 解析邮件
        email_message = email.message_from_bytes(raw_email)

        # 获取邮件主题
        subject = decode_header(email_message['Subject'])[0][0]
        if isinstance(subject, bytes):
            # 如果是bytes类型，需要进行解码
            subject = subject.decode()
        print("Subject:", subject)

        # try:
        #     typ, data = mail.search(None, 'ALL')
        # except Exception as e:
        #     print("An error occurred: ", str(e))
        # result, data = mail.uid('search', None, "UNSEEN")  # (ALL/UNSEEN)
        # i = len(data[0].split())  # emails count
        # found = ""
        # for x in range(i):
        #     latest_email_uid = data[0].split()[x]
        #     result, email_data = mail.uid('fetch', latest_email_uid, '(RFC822)')
        #     raw_email_string = email_data[0][1].decode('utf-8')
        #     email_message = email.message_from_string(raw_email_string)
        #
        #     # Header Details
        #     date_tuple = email.utils.parsedate_tz(email_message['Date'])
        #     if date_tuple:
        #         local_date = datetime.datetime.fromtimestamp(email.utils.mktime_tz(date_tuple))
        #         local_message_date = "%s" % (str(local_date.strftime("%a, %d %b %Y %H:%M:%S")))
        #     email_from = str(email.header.make_header(email.header.decode_header(email_message['From'])))
        #     email_to = str(email.header.make_header(email.header.decode_header(email_message['To'])))
        #     subject = str(email.header.make_header(email.header.decode_header(email_message['Subject'])))
        #     # Body details
        #     if subject == "Email verification":
        #         for part in email_message.walk():
        #             print(subject)
        #             if part.get_content_type() == "text/html":
        #                 body = part.get_payload(decode=True)
        #                 m = re.search('Verification code - (.+?)<br', body.decode('utf-8'))
        #                 found = m.group(1)
        #                 break
        #             else:
        #                 continue
        #         break
        # print("found: ", found)
        # return found